import React from 'react';
import { Select } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import { queryRecommendPositionList, queryRecommendCodePositionList } from '@/pages/Recruitment/service';

const SelectRecommendPost = React.forwardRef(({ talentCode, resumeId, callbackForExist, ...rest }, ref) => {
    // 岗位列表
    const { data: postList } = useRequest(
        () => {
            if (talentCode) return queryRecommendCodePositionList(talentCode);
            if (resumeId) return queryRecommendPositionList(resumeId);
            return Promise.resolve({ code: 10000, data: [] });
        },
        {
            initialData: [],
            refreshDeps: [resumeId, talentCode],
            ...defaultFormatResult
        }
    );

    const isExist = rest?.value && postList.some(item => item.recruitId === rest.value && item.existState);

    React.useEffect(() => {
        if (callbackForExist) callbackForExist(isExist);
    }, [isExist]);

    return (
        <>
            <Select
                showSearch
                style={{ width: 300 }}
                placeholder="请选择推荐岗位 - 部门（招聘负责人）"
                optionFilterProp="children"
                getPopupContainer={triggerNode => triggerNode.parentElement}
                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                ref={ref}
                dropdownMatchSelectWidth={false}
                dropdownStyle={{ maxWidth: 550 }}
                {...rest}
            >
                {postList?.map(item => (
                    <Select.Option value={item.recruitId} key={item.recruitId}>
                        {`${item.name}（${item.principalEmployeeName || '--'}）`}
                    </Select.Option>
                ))}
            </Select>
            {isExist && <div style={{ color: 'red' }}>当前候选人已投递此招聘岗位，请勿重复推荐</div>}
        </>
    );
});

export default SelectRecommendPost;
